import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';
import render from '@feidao/web/atom/nodejs/render/render';

import name2title from './fields';
import { Params } from '../atom/params2query';
import p2q from '../atom/params2query';
import generate_query4render from '../atom/generate-query4render';
import generate_sort_fields from '../atom/generate-sort-fields';

import P01 from './P01';
import P02 from './P02';
import P04 from './P04';
import P10 from './P10';
import P13 from './P13';

function create_data_p10(fd: IFeidaoAiNodejsComponent) {
	try {
		const fields = ['staff_name', 'sex', 'staff_type', 'birthday', 'f_name', 'politics_face', 'entry_party_time', 'education', 'bachelo_ocience'];
		const data = generate_sort_fields(fd.data.url, fd.data.params as Params, fields);
		data.forEach((d) => {
			const field = d.field;
			render(fd.data.node, d, P10, field);
		});
	} catch (err) {
		console.error('------err', err);
	}
}

export default async function nba001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	let page_no = fd.data.params['page_no'];
	if (!page_no) {
		page_no = 1;
	}
	let page_size = fd.data.params['page_size'];
	if (!page_size) {
		page_size = 20;
	}
	const q = p2q(fd.data.params as Params);
	const query = {
		criteria: q.criteria,
		sort: q.sort,
		page_no,
		page_size
	};
	const data = generate_query4render(fd.data.url, fd.data.params as Params, name2title);
	render(fd.data.node, data, P01, 'P01');
	create_data_p10(fd);
	const res = await nodejs(fd.data.spaceid, "teach_staff_find", query);
	if (res.dataArray.length == 0) {
		render(fd.data.node, res.dataArray, P04, 'P02');
	} else {
		render(fd.data.node, res.dataArray, P02, 'P02');
	}
	res['page_size'] = page_size;
	render(fd.data.node, res, P13, 'P13');
}
